import { queryUsers,  } from "@/services/user";
import { shareFlie } from "../service";

export default {
    namespace: 'fileDetail',
    state: {
        currentUser: {},
        userList: [],
        isMobile: false,
        modalVisible: false,
    },
    effects: {
        *fetchUserList(_, {call, put}) {
            const response = yield call(queryUsers, {},true);
            // console.log("==>> response", response)
            if (response.results) {
                yield put({
                    type: 'UpdateUserList',
                    payload: response.results
                })
            }
        },

        *shareFile(params, { call, put}) {
            const { fields, vdiskName, file } = params.payload;
            // console.log("==>> shareFile", fields.expires.endOf('day').fromNow())
            const _expires = fields.expires.endOf('day').fromNow().split(' ')
            const unit = _expires.pop();
            let interval = Number(_expires.pop());
            if (!interval) {
                console.error("interval==> ", interval)
                interval = 1;
            }
            const args = { 
                userid: Number(fields.user[0]), 
                disk: vdiskName, 
                type: fields.type, 
                path: file.path, 
                unit: unit,
                expires: interval,
            }
            console.log("==>> args", args)
            const response = yield call(shareFlie, args);
            console.log("==>> response", response)
            if (response.ok) {
                yield put({
                    type: 'UpdateModalVisible',
                    payload: false
                })
            }
        },
    },
    reducers: {
        UpdateModalVisible(state, action) {
            return { ...state, modalVisible: action.payload || false}
        },

        UpdateUserList(state, action) {
            return { ...state, userList: action.payload || [] }
        },
    },
}